<?php
namespace app\index\controller;

use app\common\Base;
use app\common\HttpClient;
use app\index\model\Business;
use app\index\model\Commodity;
use app\index\model\CommodityAppraises;
use app\index\model\Coupon;
use app\index\model\Order;
use think\Exception;
use think\Request;

class Index extends Base
{
    /**
     * @youLike 首页-猜你喜欢显示
     * @return \think\response\Json
     * @throws Exception
     */
    public function youLike(Business $business,Commodity $commodity){
        try {
            $businessData = $business->youLikeBusiness();
            $newBusinessData = [];
            foreach ($businessData as $key => $value){
                $count = $commodity->youLikeCount($businessData[$key]['id']);
                if ($count >= 3){
                    array_push($newBusinessData,$value);
                }
            }
            shuffle($newBusinessData);
            $arr = array_rand($newBusinessData,1);
            $youBusiness = $newBusinessData[$arr];

            $commodityData = json_decode($commodity->youLikeCommodity($youBusiness['id']),true);
            shuffle($commodityData);
            $commodityArr = array_rand($commodityData,3);
            $youCommodity = [];
            foreach ($commodityArr as $key => $value){
                array_push($youCommodity,$commodityData[$value]);
                $youCommodity[$key]['id'] = $youBusiness['id'];
                $youCommodity[$key]['businessName'] = $youBusiness['businessName'];
            }
            return responseJson(Base::OK,'数据获取成功',$youCommodity);
        }catch (Exception $e){
            throw $e;
        }
    }

    /**
     * @rankingList 首页-排行榜显示
     * @param Business $business
     * @return \think\response\Json
     */
    public function rankingList(Business $business){
        try {
            if (empty($data = $business->rankingList())) {
                return responseJson(Base::FAIL,'获取失败，请重试');
            }
            return responseJson(Base::OK,'数据获取成功',$data);
        }catch (Exception $e){
            throw $e;
        }
    }

    /**
     * @getCoupons  领劵中心
     * @param Coupon $coupon
     * @return \think\response\Json
     */
    public function getCoupons(Coupon $coupon){
        try {
            if (!$data = json_decode($coupon->findCoupon(),true)) {
                return responseJson(Base::FAIL,'数据获取失败,请重试');
            }
            if (empty($data)){
                return responseJson(Base::OK,'暂无优惠券');
            }
            $couponData = [];
            if (count($data) < 3){
                $couponData = $data;
            }else{
                $arr = array_rand($data,3);
                shuffle($arr);
                foreach ($arr as $value){
                    array_push($couponData,$data[$value]);
                }
            }
            return responseJson(Base::OK,'数据获取成功',$couponData);
        }catch (Exception $e){
            throw $e;
        }
    }

    /**
     * @setMeal 首页-今日套餐
     * @param Request $request
     * @param Commodity $commodity
     * @param Business $business
     * @return \think\response\Json
     * @throws Exception
     */
    public function setMeal(Request $request,Commodity $commodity,Business $business){
        try {
            $position = $request->post('position');
            $position = (string)$position;
            if (!$nearBusiness = $business->setMalSel('id,businessName',$position)){
                return responseJson(Base::OK,'数据获取失败，请重试');
            }
            foreach ($nearBusiness as $key => $val){
                $commodityCount = $commodity->youLikeCount($nearBusiness[$key]['id']);
                if ($commodityCount < 1){
                    unset($nearBusiness[$key]);
                }
            }
            $businessData = [];
            if (count($nearBusiness) < 4){
                $businessData = $nearBusiness;
            }else{
                $arr = array_rand($nearBusiness,4);
                shuffle($arr);
                foreach ($arr as $value){
                    array_push($businessData,$nearBusiness[$value]);
                }
            }
            foreach ($businessData as $key => $value){
                if (!$allCommodity = json_decode($commodity->businessCommodity($businessData[$key]['id']),true)) {
                    return responseJson(Base::OK,'数据获取失败，请重试');
                }
                shuffle($allCommodity);
                $commodityData = array_rand($allCommodity,1);
                $businessData[$key]['commodityId'] = $allCommodity[$commodityData]['id'];
                $businessData[$key]['commodityName'] = $allCommodity[$commodityData]['commodityName'];
                $businessData[$key]['commodityImg'] = $allCommodity[$commodityData]['commodityImg'];
                $businessData[$key]['originalPrice'] = $allCommodity[$commodityData]['originalPrice'];
                $businessData[$key]['presentPrice'] = $allCommodity[$commodityData]['presentPrice'];
            }
            return responseJson(Base::OK,'数据获取成功',$businessData);
        }catch (Exception $e){
            throw $e;
        }
    }

    /**
     * @foundBusiness   首页-发现好店
     * @param Request $request
     * @param Business $business
     * @param CommodityAppraises $commodityAppraises
     * @param Order $order
     * @return \think\response\Json
     * @throws Exception
     */
    public function foundBusiness(Request $request,Business $business,CommodityAppraises $commodityAppraises,Order $order){
        try {
            $position = $request->post('position');
            $position = (string)$position;
            if (!$nearBusiness = $business->setMalSel('id,businessName,businessImg,businessRange',$position)) {
                return responseJson(Base::OK,'数据获取失败,请重试');
            }
            $businessData = [];
            if (count($nearBusiness) < 4){
                $businessData = $nearBusiness;
            }else{
                $arr = array_rand($nearBusiness,4);
                shuffle($arr);
                foreach ($arr as $value){
                    array_push($businessData,$nearBusiness[$value]);
                }
            }
            foreach ($businessData as $i => $val){
                //五星评价
                $businessData[$i]['Evaluation'] = ceil(($commodityAppraises->commodityScore($businessData[$i]['id']) + $commodityAppraises->serviceScore($businessData[$i]['id']) + $commodityAppraises->timeScore($businessData[$i]['id']))/3);
                //人均
                $businessData[$i]['perCapita'] = number_format($order->avgBusiness($businessData[$i]['id']),1);
            }
            return responseJson(Base::OK,'数据获取成功',$businessData);
        }catch (Exception $e){
            throw $e;
        }
    }

    /**
     * @nearBusiness    首页-附近商铺
     * @param Request $request
     * @param Business $business
     * @return \think\response\Json
     * @throws \think\db\exception\BindParamException
     * @throws \think\exception\PDOException
     */
    public function nearBusiness(Request $request,Business $business,CommodityAppraises $commodityAppraises,Order $order){
        $position = $request->post('position');
        $position = (string)$position;
        if (!$nearBusiness = $business->setMalSel('id,businessName,businessImg,businessRange',$position)) {
            return responseJson(Base::OK,'数据获取失败,请重试');
        }
        foreach ($nearBusiness as $i => $val){
            //五星评价
            $nearBusiness[$i]['Evaluation'] = ceil(($commodityAppraises->commodityScore($nearBusiness[$i]['id']) + $commodityAppraises->serviceScore($nearBusiness[$i]['id']) + $commodityAppraises->timeScore($nearBusiness[$i]['id']))/3);
            //人均
            $nearBusiness[$i]['perCapita'] = number_format($order->avgBusiness($nearBusiness[$i]['id']),1);
            //距离
            $nearBusiness[$i]['distance'] = '4.5km';
        }
        shuffle($nearBusiness);
        return responseJson(Base::OK,'附近商家',$nearBusiness);
    }
    public  function  bugYlp(Request $request){
        $ip = $request->post('ip');
        $url = "https://restapi.amap.com/v3/ip?ip=$ip&output=json&key=6eeea94cca86ed8d0c8cb9732ec4c07a";
        $value =  HttpClient::get($url);
        $value = json_decode($value,true);

        return responseJson(Base::OK,'ok',$value);
    }
}
